package com.loyal.loyalerp.humenresource.role.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.company.model.GyCompany;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.humenresource.role.mapper.GyRoleMapper;
import com.loyal.loyalerp.humenresource.role.model.GyRole;
import com.loyal.loyalerp.humenresource.role.service.RoleService;
import com.loyal.loyalerp.model.vo.GridVO;
import com.loyal.loyalerp.utils.ValidateUtil;

@Controller
@RequestMapping("/Role")
public class RoleController {
	@Autowired
	RoleService roleService;
	@Autowired
	GyRoleMapper gyRoleMapper;
	
	final static String tableName = "gy_role";
	
	@RequestMapping("/jsp")
	public String listjsp() {
		return "basedata/Role/role";
	}
	
	@RequestMapping("/editjsp")
	public String editjsp(HttpServletRequest request) {
		List<GyCompany> companys=gyRoleMapper.companyList();
		List<GyRole> roles=gyRoleMapper.alllistRole();
		request.setAttribute("companys",companys);
		request.setAttribute("roles", roles);
		return "basedata/Role/editRole";
	}
	
	@RequestMapping("/searchjsp")
	public String searchjsp() {
		return "basedata/Role/search";
	}
	
	@RequestMapping("/listRole")//查 顯示全部
	@ResponseBody
	public Object listRole(String _search, Integer rows, Integer page, String sord,String searchField, String searchString,String searchOper) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		
		if (!ValidateUtil.validateQuery(tableName)) {
			return ServerResponse.createByError("没有权限");
		}
		
		if ("false".equals(_search))// 列表显示
		{
			PageInfo<GyRole> pageInfo = roleService.listRole(page, rows, sord);
			
			return GridVO.fromPageInfo(pageInfo);
		} 
		//模糊查询
		else if("true".equals(_search)){
			PageInfo<GyRole> pageInfo = roleService.findRole(page, rows, sord, searchField, searchString,searchOper);
			return GridVO.fromPageInfo(pageInfo);
		}
		
		return null;
	}
	

	
	@RequestMapping("/editRole") // 增删改
	@ResponseBody
	public Map<String,Object> editRole(GyRole gyRole,String companyname,String oper) throws IOException {
		Map<String,Object> map=new HashMap<String, Object>();
		
			try {
				String msg = roleService.editRole(gyRole,companyname, oper);
				if("success".equals(msg)) {
					map.put("status", 200);
				}else if("no permission".equals(msg)){
					map.put("status", 888);
				}
			}catch (Exception e) {
				map.put("msg", "查看信息是否填写正确");
			}
			return map;
		
	}
	
	
}
